package com.by.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class SqlSessionFactoryUtil {
    private static SqlSessionFactory factory;
    private SqlSessionFactoryUtil(){
    }
    static{
        Reader reader = null;
        try{
            reader = Resources.getResourceAsReader("Mybatis.xml");
            factory = new SqlSessionFactoryBuilder().build(reader);
        }catch(IOException e){
            throw new RuntimeException(e);
        }finally{
            try{
                if(reader != null){
                    reader.close();
                }
            }catch(IOException e){
                throw new RuntimeException(e);
            }
        }
    }
    public static SqlSession getSqlSession(){
        return factory.openSession(true);
    }//true代表自动提交事务
}
